home *** CD-ROM | disk | FTP | other *** search
- /**\
- |**| =====================================================================
- |**|
- |**| Justify Layout.c
- |**|
- |**| This file contains the calls that this sample needs to make
- |**| the QuickDraw GX shell work correctly.
- |**|
- |**| QuickDraw GX Libraries Used:
- |**| "color library.c", "font library.c", "graphics debug library.c",
- |**| "layout library.c", "shape library.c", and "transform library.c".
- |**|
- |**| ©1992-1994 Apple Computer, Inc.
- |**| All rights reserved.
- |**|
- |**| =====================================================================
- \**/
-
-
- #include "QDGX shell.h"
- #include "layout routines.h"
- #include "layout library.h"
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| PROTOTYPES
- |**| ---------------------------------------------------------------------
- \**/
-
- // funtions required by shell
-
- void DoSetup (void);
- void DoDraw (WindowPtr wind, Boolean updating);
- OSErr DoCreateNew (void);
- void DoDispose (WindowPtr wind);
- void DoIdle (WindowPtr wind);
- void DoTeardown (void);
- void DoClick (WindowPtr wind, Point p);
-
- // private functions
-
- OSErr DoWindowInit (WindowPtr wind);
- void CreateSampleImage (WindowPtr wind);
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| ENUMS
- |**| ---------------------------------------------------------------------
- \**/
- enum { rWindResource = 128 };
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| GLOBALS
- |**| ---------------------------------------------------------------------
- \**/
- // If gDebugging = TRUE, graphics library errors and notices will be posted. This
- // functionality will only work with the "debugging" version of QuickDraw GX.
- // If the debugging version is not installed, nothing bad will happen, but these
- // functions will not work.
-
- Boolean gDebugging = true;
-
- // Set "gGiveMeValidation" to TRUE if you want receive run-time validation.
-
- Boolean gGiveMeValidation = true;
-
-
- // gGraphicsHeapSize sets the size of the graphics heap created by calling the
- // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c. You can determine
- // the amount of graphics heap required by using GraphicsBug. I'm giving it 600K,
- // since we need abunch if we have several windows open.
-
- long gGraphicsHeapSize = 600;
-
- // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
- // override. This is so that our override can be native PowerPC code if necessary.
-
- GXPrintingEventUPP gOurPrintingOverrideUPP;
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoSetup()
- |**| Here's where we initialize any global variables our application needs.
- |**| We have only one at this time -- the universal proc pointer for
- |**| our printing override.
- |**| ---------------------------------------------------------------------
- \**/
- void DoSetup (void)
- { // Initialize our printing event override UPP
- gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoDraw()
- |**| Draw the contents of the window. The first parameter is the window
- |**| to draw, and the second parameter is true if we're updating an existing
- |**| image. If that's the case, we don't want to change anything, but
- |**| just draw what's already there.
- |**| ---------------------------------------------------------------------
- \**/
- void DoDraw (WindowPtr wind, Boolean updating)
- {
- #pragma unused (updating)
- GXDrawShape (GetDocShape(wind));
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoCreateNew()
- |**| This routine is called when a window needs to be created.
- |**| ---------------------------------------------------------------------
- \**/
- OSErr DoCreateNew (void)
- {
- OSErr err = noErr;
- WindowPtr wind;
-
- // Get and create our window from the resource fork
-
- wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
-
- // Attach a default gxViewPort to it, create and iInitialize our
- // private data for it, and add a sample image to its page shape.
-
- if ( wind == NULL )
- return (MemError());
-
- GXIgnoreGraphicsNotice(transform_already_set);
- SetDefaultViewPort(GXNewWindowViewPort(wind));
- GXPopGraphicsNotice();
-
- err = DoWindowInit(wind);
- if ( err != noErr )
- return err;
-
- CreateSampleImage(wind);
- return err;
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoDispose()
- |**| This routine is called when a window needs to be disposed of.
- |**| ---------------------------------------------------------------------
- \**/
- void DoDispose (WindowPtr wind)
- {
- TH_Doc doc;
-
- // You should always dispose of your GX graphics objects before tossing your window.
- // Why? It's generally good form and this approach guarantees that everything is
- // disposed. If you had not disposed of everything, the call to DisposeWindow should
- // dispose of the objects. If you are running the debugging version of QuickDraw GX
- // with notices set, you will receive a notice that you had not disposed of everything.
- // You can turn notices on in this file by setting gDebugging = TRUE (above).
-
- if ( wind != NULL )
- {
- doc = (TH_Doc)GetWRefCon(wind); // Remember, this is where we stored our private data.
- GXDisposeShape(GetDocShape(wind)); // Dispose of this doc's shape.
- GXDisposeJob(GetDocJob(wind)); // Dispose of this doc's print job.
- DisposHandle((Handle) doc); // Dispose of our private data.
- DisposeWindow(wind); // Dispose of the window.
- }
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoIdle()
- |**| This routine is called to do things while idling through the event loop.
- |**| ---------------------------------------------------------------------
- \**/
- void DoIdle (WindowPtr wind)
- {
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoTeardown()
- |**| This routine is called just before we quit to remove anything
- |**| persistent that might have been setup by DoSetup().
- |**| ---------------------------------------------------------------------
- \**/
- void DoTeardown (void)
- {
- DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoClick()
- |**| ---------------------------------------------------------------------
- \**/
- void DoClick(WindowPtr window, Point p)
- {
- }
-
-
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoWindowInit()
- |**| In this function we create and initialize the the private document
- |**| structure for a new window. This structure contains the print job and
- |**| the shape which is drawn in the window. We store this data in a handle
- |**| and hang it off the window's refCon field for easy retrieval. By doing
- |**| this, rather than using globals, we can create many windows containing
- |**| unique print jobs and shapes.
- |**| ---------------------------------------------------------------------
- \**/
- OSErr DoWindowInit (WindowPtr wind)
- {
- OSErr err = noErr;
- gxJob docJob;
- gxShape docPage;
- TH_Doc windDoc;
-
-
- // Create the page shape. We set the unique items attribute to make sure that each item
- // added to the picture has a unique reference. If this attribute was not set, we would
- // not see all copies of anything we add to the shape multiple times -- we'd just see
- // the last version added.
-
- docPage = GXNewShape(gxPictureType);
- GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
-
-
- // Create a print job for this document. This will be the same as the system default until
- // the user goes through the dialogs for Page Setup or Print…
-
- err = GXNewJob(&docJob);
-
-
- // If there are no errors, create a handle the size of our document structure and store
- // the print job and page shape in it. Store the handle in the window's refCon field so
- // that we can get at it. (Note that the utility routines "GetDocJob" and "GetDocShape"
- // can be used to do this easily.
-
- if ( err == noErr )
- {
- windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
-
- if ( windDoc == NULL )
- err = MemError();
- else
- {
- (*windDoc)->docJob = docJob;
- (*windDoc)->docPage = docPage;
- SetWRefCon(wind, (long) windDoc);
- }
-
- // Now install our application override for PrintingEvent so that we can
- // support the new movable-modal printing dialog boxes.
-
- GXInstallApplicationOverride(docJob, gxPrintingEvent, gOurPrintingOverrideUPP);
-
- }
-
- return err;
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| CreateSampleImage()
- |**| This function creates primitive shapes and adds them to the window's page shape.
- |**| ---------------------------------------------------------------------
- \**/
- void CreateSampleImage (WindowPtr wind)
- {
- Rect ourWindowRect = wind->portRect; // the rectangle for this window
- // in QuickDraw coordinates
- gxShape layout1; // the first layout shape we build
- gxShape layout2; // the second layout shape we build
- gxShape layout3; // the third layout shape we build
- gxShape layout4; // the fourth layout shape we build
- gxShape thePage; // this window's document shape
- gxPoint posn; // position for our shapes
- gxRunControls runControls; // run controls for the layout shape
- gxLayoutOptions layoutOptions; // options for the layout shape
- StyleRunOverrides overrides; // style run overrides for our shapes
-
- // We use these two structures to assign all justification priority either
- // all to the space characters (allToSpace) or all to intercharacter spacing
- // (allToChar).
-
- gxPriorityJustificationOverride allToSpace, allToChar;
- //
-
- char * text1 = "QuickDraw GX Line Layout is a set of support";
- char * text2 = "routines that applications call to implement";
- char * text3 = "certain functions. It is not a text editor or";
- char * text4 = "word processor, but it could help make one.";
-
-
- // make default gxLayoutOptions and gxRunControls structures. These routines are
- // found in layout library.c.
-
- InitializeLayoutOptions (&layoutOptions);
- InitializeRunControls (&runControls);
- InitializeStyleRunOverrides(&overrides);
-
- // Initialize default gxPriorityJustOverrides structures with another handy
- // routine found in layout library.c.
-
- SetDefaultPriorityJustOverride(&allToSpace);
- SetDefaultPriorityJustOverride(&allToChar);
-
- // For the allToSpace structure, we set the unlimited override flag as well as
- // unlimited gap absorption, which forces all extra width introduced during
- // justification to go to the white-space characters.
-
- allToSpace.deltas[gxWhiteSpacePriority].growFlags |=
- (gxOverrideUnlimited | gxUnlimitedGapAbsorption);
-
- // For the allToChar structure, we set the unlimited override flag as well as
- // overriding the inter-character priority to "kashida," which is the highest
- // priority. This guarantees that inter-character spacing is adjusted when
- // justifying a line.
-
- allToChar.deltas[gxInterCharPriority].growFlags |=
- (gxOverrideUnlimited | gxUnlimitedGapAbsorption |
- gxOverridePriority | gxKashidaPriority);
-
- // We set the text to about twice the width really needed
- // so that the justification effects are clearly visible.
-
- layoutOptions.width = ff(6.5 * 72);
- layoutOptions.just = fract1; // full justification
-
- // To position the lines, we compute the position that would center a
- // layout's baseline in the window. We'll draw the lines 30 points apart,
- // so we'll put the first two 45 and 15 points above this position and the
- // second two 15 and 45 points below it.
-
- posn.x = (ff(ourWindowRect.right - ourWindowRect.left)
- - layoutOptions.width) / 2;
- posn.y = ff((( ourWindowRect.bottom - ourWindowRect.top) / 2) - 45);
-
-
- // Setup complete! Build the layouts, using NewSingleLayout from layout library.c
- // to create layouts with single style runs. The first and second layouts have no
- // options, but the third one has all justification in spaces and the fourth has
- // all justification in inter-character spacing.
-
- GXIgnoreGraphicsNotice(text_size_already_set);
- layout1 = NewSingleLayout(
- text1, // the text for the layout
- (char *) "\pHoefler Text", // the font name to use
- ff(12), // the point size to use
- &layoutOptions, // our default layout options
- &posn, // the shape's position
- 0, // no text attributes
- &runControls, // our default run controls
- nil, // the run features array (none)
- 0, // count of zero features = 0
- nil); // no style run overrides
- GXPopGraphicsNotice();
- posn.y += ff(30); // move down 30 pixels
-
- GXIgnoreGraphicsNotice(text_size_already_set);
- layout2 = NewSingleLayout(
- text2, // the text for the layout
- (char *) "\pHoefler Text", // the font name to use
- ff(12), // the point size to use
- &layoutOptions, // our default layout options
- &posn, // the shape's position
- 0, // no text attributes
- &runControls, // our default run controls
- nil, // the run features array (none)
- 0, // count of zero features = 0
- nil); // no style run overrides
- GXPopGraphicsNotice();
-
- posn.y += ff(30); // move down 30 pixels
- overrides.priorityJustOverride = &allToSpace;
-
- GXIgnoreGraphicsNotice(text_size_already_set);
- layout3 = NewSingleLayout(
- text3, // the text for the layout
- (char *) "\pHoefler Text", // the font name to use
- ff(12), // the point size to use
- &layoutOptions, // our default layout options
- &posn, // the shape's position
- 0, // no text attributes
- &runControls, // our default run controls
- nil, // the run features array (none)
- 0, // count of zero features = 0
- &overrides); // our style run overrides
- GXPopGraphicsNotice();
-
- posn.y += ff(30); // move down 30 pixels
- overrides.priorityJustOverride = &allToChar;
-
- GXIgnoreGraphicsNotice(text_size_already_set);
- layout4 = NewSingleLayout(
- text4, // the text for the layout
- (char *) "\pHoefler Text", // the font name to use
- ff(12), // the point size to use
- &layoutOptions, // our default layout options
- &posn, // the shape's position
- 0, // no text attributes
- &runControls, // our default run controls
- nil, // the run features array (none)
- 0, // count of zero features = 0
- &overrides); // our style run overrides
- GXPopGraphicsNotice();
-
-
- // Retrieve the page shape so we can add to it.
-
- thePage = GetDocShape(wind);
-
- // Add the layouts to the window's picture shape. AddToShape is in shape library.c.
-
- AddToShape(thePage, layout1);
- AddToShape(thePage, layout2);
- AddToShape(thePage, layout3);
- AddToShape(thePage, layout4);
-
- // Create two lines to show the margins of these lines, and add them to the picture
- // shape.
-
- {
- gxLine bound; // lines showing edges of layouts
- gxShape lineShape; // shape to hold our lines
-
- bound.first.x = posn.x;
- bound.first.y = posn.y - ff(135);
- bound.last.x = posn.x;
- bound.last.y = posn.y + ff(15);
- lineShape = GXNewLine(&bound);
- AddToShape(thePage, lineShape);
- GXDisposeShape(lineShape);
-
- bound.first.x += layoutOptions.width;
- bound.last.x += layoutOptions. width;
- lineShape = GXNewLine(&bound);
- AddToShape(thePage, lineShape);
- GXDisposeShape(lineShape);
- }
-
- // Now that all shapes are added to the picture, dispose of them
-
- GXDisposeShape(layout1);
- GXDisposeShape(layout2);
- GXDisposeShape(layout3);
- GXDisposeShape(layout4);
-
-
- // Invalidate the window's portRect so that everything gets updated.
-
- SetPort(wind);
- InvalRect(&ourWindowRect);
- }
-
-